﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace SmartSpace.WebApp._console
{
    public partial class _console : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            _ctlLabelDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

            XDocument xDoc = XDocument.Load(Server.MapPath("~/_console/menu.config"));
            XElement menu = xDoc.Element("menu");

            _ctlTreeMenu.Nodes.Clear();
            foreach (var item in menu.Elements("menu"))
            {
                TreeNode node = new TreeNode();
                node.Text = item.Attribute("caption").Value;
                node.NavigateUrl = item.Attribute("url").Value;
                if (node.NavigateUrl == "#")
                {
                    node.NavigateUrl = string.Empty;
                    node.SelectAction = TreeNodeSelectAction.Expand;
                }

                LoadMenuItems(node, item);
                _ctlTreeMenu.Nodes.Add(node);
            }
        }

        private void LoadMenuItems(TreeNode parentNode, XElement parentelement)
        {
            foreach (var item in parentelement.Elements("menu"))
            {
                TreeNode node = new TreeNode();
                node.Text = item.Attribute("caption").Value;
                node.NavigateUrl = item.Attribute("url").Value;
                if (node.NavigateUrl == "#")
                {
                    node.NavigateUrl = string.Empty;
                    node.SelectAction = TreeNodeSelectAction.Expand;
                }
                parentNode.ChildNodes.Add(node);
            }
        }
    }
}